package admin

import (
	_ "embed"
	. "go-caipu/pkg/services/sqlstore/migrator"
)

//go:embed sql/sys_role.sql
var roleSQLByte []byte

func AddRoleMigrations(mg *Migrator) {
	roleV1 := Table{
		Name: "sys_role",
		Columns: []*Column{
			{Name: "role_id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: false},
			{Name: "role_name", Type: DB_Varchar, Length: 128, Nullable: false},
			{Name: "status", Type: DB_Int, Length: 4, Nullable: false},
			{Name: "role_key", Type: DB_NVarchar, Length: 64, Nullable: false},
			{Name: "sort", Type: DB_Int, Length: 4, Nullable: true},
			{Name: "flag", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "remark", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "admin", Type: DB_Int, Length: 4, Nullable: true},
			{Name: "data_scope", Type: DB_NVarchar, Length: 255, Nullable: true},

			{Name: "create_by", Type: DB_BigInt, Nullable: false},
			{Name: "update_by", Type: DB_BigInt, Nullable: true},
			{Name: "created_at", Type: DB_DateTime, Nullable: false},
			{Name: "updated_at", Type: DB_DateTime, Nullable: true},
		},
		Indices: []*Index{
			{Cols: []string{"role_name"}, Type: UniqueIndex},
			{Cols: []string{"role_key"}, Type: UniqueIndex},
		},
	}

	//create table
	mg.AddMigration("create sys_role table", NewAddTableMigration(roleV1))
	//insert default role
	mg.AddMigration("insert into  sys_role", NewRawSQLMigration(string(roleSQLByte)))

	//角色的菜单
	roleMenuV1 := Table{
		Name: "sys_role_menu",
		Columns: []*Column{
			{Name: "role_id", Type: DB_BigInt, Nullable: false},
			{Name: "menu_id", Type: DB_BigInt, Nullable: false},
		},
		Indices: []*Index{
			{Cols: []string{"role_id"}, Type: UniqueIndex},
			{Cols: []string{"menu_id"}, Type: UniqueIndex},
		},
	}
	mg.AddMigration("create table sys_role_menu", NewAddTableMigration(roleMenuV1))
	//角色-部门数据查看权限
	roleDeptV1 := Table{
		Name: "sys_role_dept",
		Columns: []*Column{
			{Name: "role_id", Type: DB_BigInt, Nullable: false},
			{Name: "dept_id", Type: DB_BigInt, Nullable: false},
		},
		Indices: []*Index{
			{Cols: []string{"role_id"}, Type: UniqueIndex},
			{Cols: []string{"dept_id"}, Type: UniqueIndex},
		},
	}
	mg.AddMigration("create table sys_role_dept", NewAddTableMigration(roleDeptV1))

}
